package org.minos.edit
{
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import org.aswing.ASColor;
	import org.aswing.AsWingManager;
	import org.aswing.BorderLayout;
	import org.aswing.Container;
	import org.aswing.Insets;
	import org.aswing.JPanel;
	import org.aswing.JTabbedPane;
	import org.aswing.JWindow;
	import org.aswing.plaf.ComponentUI;
	import org.aswing.plaf.InsetsUIResource;
	import org.aswing.SoftBoxLayout;
	import org.aswing.UIManager;
	import org.aswing.WindowLayout;
	import org.ichow.version.VersionInfo;
	import org.minos.edit.events.EditEvent;
	import org.minos.edit.version.Version;
	import org.minos.edit.view.components.InfoPane;
	import org.minos.edit.view.components.InitInfoPane;
	import org.minos.edit.view.components.Menu;
	import org.minos.edit.view.components.SceneTable;
	import org.minos.edit.view.components.ToolBar;
	
	/**
	 * ...
	 * @author Minos
	 */
	[SWF( width="1100",height="700" )]
	
	public class Main extends Sprite
	{
		public static const NAME : String = "Edit";
		public static const DRAG_DROP : String = NAME + "_DragDrop";
		
		public var menuContainer : Menu;
		public var mainWindow : JWindow;
		public var toolContainer : ToolBar;
		public var sceneContainer : SceneTable;
		public var infoContainer : InfoPane;
		public var initInfoContainer : InitInfoPane;
		public var assetsPanel : JPanel;
		
		public function Main() : void
		{
			if ( stage )
				init();
			else
				addEventListener( Event.ADDED_TO_STAGE , init );
		}
		
		private function init( e : Event = null ) : void
		{
			removeEventListener( Event.ADDED_TO_STAGE , init );
			initAswing()
			EditFacate.getInstance().startup( this );
		}
		
		private function initAswing() : void
		{
			
			VersionInfo.init( this , Version , "Edit" );
			AsWingManager.initAsStandard( this );
			menuContainer = new Menu();
			toolContainer = new ToolBar();
			infoContainer = new InfoPane();
			sceneContainer = new SceneTable();
			
			mainWindow = new JWindow();
			var pane : Container = mainWindow.getContentPane();
			
			pane.setLayout( new BorderLayout( 0 , 2 ) );
			//头部
			var top : JPanel = new JPanel( new SoftBoxLayout( SoftBoxLayout.Y_AXIS , 2 ) );
			top.append( menuContainer ); //菜单
			top.append( infoContainer ); //信息
			
			//分页
			var tabbedPane : JTabbedPane = new JTabbedPane();
			tabbedPane.setLeadingOffset( 5 );
			var ui : ComponentUI = tabbedPane.getUI();
			ui.putDefault( "TabbedPane.contentMargin" , new InsetsUIResource( 2 , 2 , 2 , 2 ) );
			tabbedPane.setUI( ui );
			
			var center : JPanel = new JPanel( new BorderLayout( 0 , 2 ) );
			center.setOpaque( true );
			center.append( toolContainer , BorderLayout.NORTH ); //工具
			center.append( sceneContainer , BorderLayout.CENTER ); //列表
			tabbedPane.appendTab( center , "场景列表" );
			//初始
			initInfoContainer = new InitInfoPane();
			assetsPanel = new JPanel( new BorderLayout() );
			assetsPanel.setOpaque( true );
			tabbedPane.appendTab( assetsPanel , "资源列表" );
			//
			pane.append( top , BorderLayout.NORTH );
			pane.append( tabbedPane , BorderLayout.CENTER );
			pane.append( initInfoContainer , BorderLayout.SOUTH );
			
			mainWindow.setSizeWH( stage.stageWidth , stage.stageHeight );
			mainWindow.show();
			
			initControllers();
		
		}
		
		private function initControllers() : void
		{
			stage.addEventListener( Event.RESIZE , __onResized );
			stage.addEventListener( NativeDragEvent.NATIVE_DRAG_DROP , __onNativeDragDrop );
			stage.addEventListener( NativeDragEvent.NATIVE_DRAG_ENTER , __onNativeDragEnter );
		}
		
		private function __onNativeDragEnter( e : NativeDragEvent ) : void
		{
			if ( e.clipboard.hasFormat( ClipboardFormats.FILE_LIST_FORMAT ) )
			{
				var files : Array = e.clipboard.getData( ClipboardFormats.FILE_LIST_FORMAT ) as Array;
				if ( files.length > 0 )
					NativeDragManager.acceptDragDrop( this );
			}
		}
		
		private function __onNativeDragDrop( e : NativeDragEvent ) : void
		{
			var file : File = ( e.clipboard.getData( ClipboardFormats.FILE_LIST_FORMAT ) as Array )[ 0 ] as File;
			
			var evt : EditEvent = new EditEvent( Main.DRAG_DROP )
			if ( file.isDirectory )
				evt.data = { type: "dir" , data: file };
			else if ( file.extension.toLowerCase() == "xml" )
				evt.data = { type: "open" , data: file };
			else
				evt.data = { type: "error" , data: null };
			dispatchEvent( evt );
		}
		
		private function __onResized( e : Event ) : void
		{
			mainWindow.setSizeWH( stage.stageWidth , stage.stageHeight );
		}
	}

}